package Fonctions;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

import InterFaces.IEvent;
import InterFaces.IModifEvent;

public class Plugin_ModifEvent implements IModifEvent {

	String eventId;
	String eventTitle;
	Date debut;
	Date fin;
	
	File Event;
    File User;
    
    FileInputStream fis = null;
	
    public Plugin_ModifEvent(String eventId,String evntTitle, Date debut, Date fin) {
  
    	this.debut = debut;
    	this.fin = fin;
    	this.eventTitle = evntTitle;
        this.eventId = eventId;
    }
	
	
    /* (non-Javadoc)
	 * @see Fonctions.IModifEvent#Modifier(InterFaces.IEvent)
	 */
    @Override
	public void Modifier(IEvent event)
    {
    	ArrayList<IEvent> listEventsUser = new ArrayList<IEvent>();
    	 File f = new File("Events.txt");

    try{
    
        //Lecture du fichier
    	try{
            //Lecture du fichier
                InputStream ips=new FileInputStream(f); 
                        InputStreamReader ipsr=new InputStreamReader(ips);
                        BufferedReader br=new BufferedReader(ipsr);
                        String ligne;
                        while ((ligne=br.readLine())!=null){
                            String[] tabString = ligne.split(",");
                            
                                    
                            IEvent ev = new Data.Event(tabString[0],new Date(Long.parseLong(tabString[2])), new Date(Long.parseLong(tabString[3])));
                            listEventsUser.add(ev);
                            
                            
                    }
                        br.close(); 
          
            }
            catch (IOException e) {
            e.printStackTrace();
         }
    	
    	// Modify
    	for(int i=0; i<listEventsUser.size(); i++){
    		if (listEventsUser.get(i).getEventIdentifiant() == eventId){
    			IEvent ev = new Data.Event(eventId,debut,fin);
    			listEventsUser.set(i,ev);
    		}
    	}
    	
    	
        // Ecriture fichier 
                    FileWriter ipsr=new FileWriter(f);
                    BufferedWriter br = new BufferedWriter(ipsr);
                    String ligne;
                   
                    for (int i=0 ; i<listEventsUser.size();i++)
                    {
                      br.write(listEventsUser.get(i)+",");	
                    
                    }
                  
                    br.close(); 

        
        }
        catch (IOException e) {
        e.printStackTrace();
     }
    
    }
    
    /* (non-Javadoc)
	 * @see Fonctions.IModifEvent#Modifier(InterFaces.IEvent)
	 */
    @Override
	public void ajouter(IEvent event)
    {
    	FileWriter writer = null;
    	String texte = event.getEventIdentifiant()+','+event.getDateDebut().getTime()+','+event.getDateFin().getTime();
    	try{
    	     writer = new FileWriter("Event.txt", true);
    	     writer.write(texte,0,texte.length());
    	}catch(IOException ex){
    	    ex.printStackTrace();
    	}finally{
    	  if(writer != null){
    	     try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	  }
    	}
    }
    
}
